package com.letoken.platform.repo.app.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.repo.app.po.Token;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;

/**
 *
 * @version 1.0
 * @author： letokenlabs
 * @date： 2021/11/24 9:55
 * 黑名单钱包
 */
public class AppWalletBlackListRedis {
    private StringRedisTemplate stringRedisTemplate;

    private AppWalletBlackListRedis() {
        stringRedisTemplate = BeanUtil.getBean(StringRedisTemplate.class);
    }

    private enum Singleton {
        INSTANCE;
        private AppWalletBlackListRedis instance;

        // JVM会保证此方法绝对只调用一次
        Singleton() {
            instance = new AppWalletBlackListRedis();
        }
    }

    String cacheKey = RedisKeyConstant.LETOKEN_USER_WALLET_BLACK;

    public static AppWalletBlackListRedis me() {
        return AppWalletBlackListRedis.Singleton.INSTANCE.instance;
    }

    public void setValue(String userId, boolean value) {
//        if (userId != null) {
//            stringRedisTemplate.opsForHash().put(cacheKey, userId, value ? "1" : "0");
//            if (value) {
//                stringRedisTemplate.opsForList().leftPush(RedisKeyConstant.LETOKEN_USER_WALLET_BLACK_LIST, userId);
//            }
//        }
    }

    /**
     * 判断是否黑名单
     * @param userId
     * @return
     */
    public boolean getValue(String userId) {
        Object value = stringRedisTemplate.opsForHash().get(cacheKey, userId);
        return value == null ? false : value.toString().equals("1");
    }

    /**
     * 获取黑名单（rightPop)
     * @return
     */
    public String getPopItem(){
        return stringRedisTemplate.opsForList().rightPop(RedisKeyConstant.LETOKEN_USER_WALLET_BLACK_LIST);
    }

    /**
     * @param key ... 多个key
     * @return
     */
    public Long del(String... key) {
        return stringRedisTemplate.opsForHash().delete(cacheKey, key);
    }
}
